package com.dianjing.dianqun.api;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dianjing.dianqun.common.ResponseData;
import com.dianjing.dianqun.common.base.BaseRestController;
import com.dianjing.dianqun.model.Channel;
import com.dianjing.dianqun.model.Content;
import com.dianjing.dianqun.model.ContentTxt;
import com.dianjing.dianqun.model.Site;
import com.dianjing.dianqun.model.User;
import com.dianjing.dianqun.service.ChannelService;
import com.dianjing.dianqun.service.ContentService;
import com.dianjing.dianqun.service.SiteService;
import com.dianjing.dianqun.service.UserService;
import com.dianjing.dianqun.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @date : 2017年10月19日
 * 
 * @author: baoyongtao
 * 
 * @see :
 * 
 */

@Api(value = "ChannelApiController", description = "栏目相关api")
@RestController
@RequestMapping("/api/channel")
public class ChannelApiController extends BaseRestController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private ContentService contentService;
	
	@Autowired
	private SiteService siteService;
	
	@Autowired
	private ChannelService channelService;

	@GetMapping("/site/{siteId}")
	@ApiOperation(value = "查询栏目列表", notes = "使用站点id查询站点所有的栏目集合")
	@ApiImplicitParams({
		@ApiImplicitParam(dataType = "int", name = "siteId", required = true, value = "站点的Id", paramType = "path"),
		@ApiImplicitParam(dataType = "int", name = "userId", required = false, value = "用户的Id", paramType = "query")
	})
	public ResponseData getChannelBySiteId(@PathVariable(required = true) Integer siteId,
		   @RequestParam(required=false ,value="userId")Integer userId,HttpServletRequest request) {
		User user = userService.selectUserBySiteId(siteId);
		if(user==null) {
			return new ResponseData(ResponseData.STATUS_ERROR, "请求错误",new String[]{});
		}
		Map<String,Object> map = Maps.newHashMap();
		Site site  =  siteService.selectSitelByUserId(user.getUserId());
		map.put("user", userService.selectByPrimaryKey(user.getUserId()));
		map.put("bothFolow",userId!=null?userService.isFolow(userId,user.getUserId()):false);
		List<Channel> channl4List = Lists.newArrayList();
		List<Channel> chnnalList = Lists.newArrayList();
		List<String> asList = Arrays.asList(new String[]{"成功案例","律师团队","律所动态","关于我们"});
		if(site!=null) {
			List<Channel> list = channelService.selectBySiteId(site.getSiteId());
			for (Channel channel : list) {
				if(asList.contains(channel.getChannelExt().getChannelName())) {
					channl4List.add(channel);
					continue;
				}
				chnnalList.add(channel);
			}
			map.put("channel4List", channl4List);
			map.put("channelList", chnnalList);
			PageHelper.startPage(1, 5);
			PageInfo<Content> info = new PageInfo<>(contentService.selectContentBySiteId(site.getSiteId()));
			List<Content> list2 = info.getList();
			for (Content content : list2) {
				ContentTxt contentTxt = content.getContentTxt();
				String txt = StringUtils.replaceHtml(contentTxt.getTxt());
				if(txt.length()>201) {
					txt = txt.substring(0,200);
				}
				contentTxt.setTxt(txt);
				content.setContentTxt(contentTxt);
			}
			map.put("contentList",info.getList() );
		}
		boolean isOk = saveSiteFlow(siteId,userId, request);
		if(!isOk) {
			//保存失败 可能要返回信息 目前 不做操作 主要发生在 发开期间
			LOG.error("Save site flow error !!");
		}
		map.put("site",site);
		return new ResponseData(ResponseData.STATUS_OK, "请求成功", map);

	}

	@GetMapping("/query/{userId}")
	@ApiOperation(value = "根据用户id查询站点栏目信息", notes = "根据用户id查询站点栏目信息")
	@ApiImplicitParam(name = "userId", value = "用户id", dataType = "int", required = true, paramType = "path")
	public ResponseData querySite(@PathVariable(required = true) Integer userId,HttpServletRequest request) {
		userId = convertUserId(userId);
		Map<String,Object> map = Maps.newHashMap();
		Site site  =  siteService.selectSitelByUserId(userId);
		map.put("user", userService.selectByPrimaryKey(userId));
		map.put("site",site);
		List<Channel> channl4List = Lists.newArrayList();
		List<Channel> chnnalList = Lists.newArrayList();
		List<String> asList = Arrays.asList(new String[]{"成功案例","律师团队","律所动态","关于我们"});
		if(site!=null) {
			List<Channel> list = channelService.selectBySiteId(site.getSiteId());
			for (Channel channel : list) {
				if(asList.contains(channel.getChannelExt().getChannelName())) {
					channl4List.add(channel);
					continue;
				}
				chnnalList.add(channel);
			}
			map.put("channel4List", channl4List);
			map.put("channelList", chnnalList);
			PageHelper.startPage(1, 5);
			PageInfo<Content> info = new PageInfo<>(contentService.selectContentBySiteId(site.getSiteId()));
			List<Content> list2 = info.getList();
			for (Content content : list2) {
				ContentTxt contentTxt = content.getContentTxt();
				String txt = StringUtils.replaceHtml(contentTxt.getTxt());
				if(txt.length()>201) {
					txt = txt.substring(0,200);
				}
				contentTxt.setTxt(txt);
				content.setContentTxt(contentTxt);
			}
			map.put("contentList",info.getList() );
			boolean isOk = saveSiteFlow(site.getSiteId(),userId, request);
			if(!isOk) {
				//保存失败 可能要返回信息 目前 不做操作 主要发生在 发开期间
				LOG.error("Save site flow error !!");
			}
		}
		return new ResponseData(ResponseData.STATUS_OK, "请求成功", map);
	}

}
